﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Services;
using System.Xml.Linq;

namespace QL
{
    /// <summary>
    /// ql_test 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class ql_test : System.Web.Services.WebService
    {
        private bool verifyUser(string  LogonNo, string Pwd)
        {
            if (LogonNo == "J_DB" && Pwd == "A123456") return true;
            return false;
        }
        public XElement xmlDeal()
        {
            string url = ConfigurationManager.AppSettings["xmlUrl"];
            if(url=="")
            {
                url = System.AppDomain.CurrentDomain.BaseDirectory + "\\QL.xml";
            }
            XDocument xDoc = XDocument.Load(url); //加载xml文件
            XElement root = xDoc.Root;//获取根节点
            return root;
        }
        [WebMethod]
        public string GetTruckInfo(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid,
                string reserve1 = "", string reserve2 = "")
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = root.Elements("GetTruckInfo");
                foreach (XElement xe in item)
                {
                    if (xe.Name == "GetTruckInfo" && xe.Attribute("Guid").Value == QRCodeOrGuid)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string AddShipmentDetail(string LogonNo, string Pwd, string TruckType, string TareOrGross,
                string QRCodeOrGuid,string CheNo,string Quantity,string TareWeight,string GrossWeight,string TareDate
                , string GrossDate, string PersonWeight, string WeighbridgeNo, string Weighbridge, string reserve1="", string reserve2=""
            )
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = root.Elements("AddShipmentDetail");
                foreach (XElement xe in item)
                {
                    if (xe.Name == "AddShipmentDetail" && xe.Attribute("Guid").Value == QRCodeOrGuid)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string UpdateStatus(string LogonNo,string Pwd,string TruckType,string QRCodeOrGuid
                    , string reserve1, string reserve2)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = root.Elements("UpdateStatus");
                foreach (XElement xe in item)
                {
                    if (xe.Attribute("Guid").Value == QRCodeOrGuid)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string CancelTruck(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = root.Elements("CancelTruck");
                foreach (XElement xe in item)
                {
                    if (xe.Attribute("Guid").Value == QRCodeOrGuid)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
    }
}
